package controller;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FrontController extends MyCoolServlet {

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
               try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(FrontController.class.getName()).log(Level.SEVERE,
                    "No se pudo establecer el encoding", ex);
        }

        if (ServletFileUpload.isMultipartContent(request)) {
            gotoNamedResource(procesarImagenServlet, request, response);
        }
        else{
            String sinPermiso = (String) request.getAttribute("sinPermiso");
            if (sinPermiso == null)
                sinPermiso = "";
            String form = request.getParameter("form");
            if (form == null || (persistenceMechanism != null
                    && persistenceMechanism.equals("none")) || sinPermiso.equals("sinPermiso")) {
                gotoURL(errorForm, request, response);
            } else if (form.equals("errorForm")) {
                gotoNamedResource(errorForm, request, response);
            } else if (form.equals("createUserServlet")) {
                gotoNamedResource(createUserServlet, request, response);
            } else if (form.equals("updateServlet")) {
                gotoNamedResource(updateServlet, request, response);
            } else if (form.equals("updateUserServlet")) {
                gotoNamedResource(updateUserServlet, request, response);
            } else if (form.equals("deleteServlet")) {
                gotoNamedResource(deleteServlet, request, response);
            } else if (form.equals("deleteUserServlet")) {
                gotoNamedResource(deleteUserServlet, request, response);
            } else if (form.equals("readServlet")) {
                gotoNamedResource(readServlet, request, response);
            } else if (form.equals("readUserServlet")) {
                gotoNamedResource(readUserServlet, request, response);
            } else if (form.equals("signInServlet")) {
                gotoNamedResource(signInServlet, request, response);
            } else if (form.equals("signOutServlet")) {
                gotoNamedResource(signOutServlet, request, response);
            } else if (form.equals("addToCarritoServlet")) {
                gotoNamedResource(addToCarritoServlet, request, response);
            } else if (form.equals("deleteCarritoServlet")) {
                gotoNamedResource(deleteCarritoServlet, request, response);
            } else if (form.equals("confirmCarritoServlet")) {
                gotoNamedResource(confirmCarritoServlet, request, response);
            } else {
                gotoURL(errorForm, request, response);
            }
        }
    }
}
